<?php
App::uses('AppController', 'Controller');
/**
 * Jobs Controller
 *
 * @property Job $Job
 */
class JobsController extends AppController {


/**
 * index method
 *
 * @return void
 */
	public function index() {
		$options = array();
		if ($this->request->is('post')) {
			if(isset($this->request->data['Job']['domain_id']) && $this->request->data['Job']['domain_id']!= ""){
				$options['joins'] = array(
				array('table' => 'domains_jobs',
							        'alias' => 'DomainsJob',
							        'type' => 'inner',
							        'conditions' => array(
							            'Job.id = DomainsJob.job_id'
				)
				),
				array('table' => 'domains',
							        'alias' => 'Domain',
							        'type' => 'inner',
							        'conditions' => array(
							            'DomainsJob.domain_id = Domain.id'
				)
				)
				);
				$options['conditions'] = array(
							    'Domain.id' => $this->request->data['Job']['domain_id']
				);
			}
			
			if(isset($this->request->data['Job']['user_id']) && $this->request->data['Job']['user_id']!= ""){
				$options['joins'] = array(
				array('table' => 'users_jobs',
										        'alias' => 'UsersJobs',
										        'type' => 'inner',
										        'conditions' => array(
										            'Job.id = UsersJobs.job_id'
				)
				),
				array('table' => 'users',
										        'alias' => 'User',
										        'type' => 'inner',
										        'conditions' => array(
										            'UsersJobs.user_id = User.id'
				)
				)
				);
				$options['conditions'] = array(
							'User.id' => $this->request->data['Job']['user_id']
				);
			}
			
			if(isset($this->request->data['Job']['status']) && $this->request->data['Job']['status'] !='') $options['conditions']['Job.status'] = $this->request->data['Job']['status'];
			$this->paginate = $options;
		}
				
		$this->Job->recursive = 1;
		$jobs = $this->paginate();
		$this->loadModel('Domain');
		$domains = $this->Domain->find('list');

		$this->loadModel('User');
		$users = $this->User->find('list');
		
		$this->set(compact('domains','jobs','users'));
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Job->id = $id;
		if (!$this->Job->exists()) {
			throw new NotFoundException(__('Invalid job'));
		}
		$this->set('job', $this->Job->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Job->create();
			$this->request->data['Job']['user_id'] = $this->Session->read('Auth.User.id');
			if ($this->Job->save($this->request->data)) {
				$this->Session->setFlash(__('The job has been saved'));
				if(isset($this->passedArgs['p']) && !empty($this->passedArgs['p'])) $this->redirect(array('controller'=>'projects','action' =>'view',$this->passedArgs['p']));
				if(isset($this->passedArgs['d']) && !empty($this->passedArgs['d'])) $this->redirect(array('controller'=>'domains','action' =>'view',$this->passedArgs['d'],'d'=>'jobs'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The job could not be saved. Please, try again.'));
			}
		}
		
		$options = null;
		
		if(isset($this->passedArgs['p']) && !empty($this->passedArgs['p'])){
			// dodajemy do domeny
			$this->Job->Project->id = $this->passedArgs['p'];
			if ($this->Job->Project->exists()) {
				$this->request->data['Project']['project_id'] = $this->passedArgs['p'];
				$options['conditions'] = array(
						'Project.id' => $this->passedArgs['p']
				);
			}
		}
		
		
		if(isset($this->passedArgs['d']) && !empty($this->passedArgs['d'])){
			// dodajemy do domeny
			$this->Job->Domain->id = $this->passedArgs['d'];
			if ($this->Job->Domain->exists()) {
				$this->request->data['Domain']['Domain'] = $this->passedArgs['d'];
				
				$options['joins'] = array(
						array('table' => 'domains_jobs',
								'alias' => 'DomainsJob',
								'type' => 'inner',
								'conditions' => array(
										'Job.id = DomainsJob.job_id'
								)
						),
						array('table' => 'domains',
								'alias' => 'Domain',
								'type' => 'inner',
								'conditions' => array(
										'DomainsJob.domain_id = Domain.id'
								)
						)
				);
				
				$options['conditions'] = array(
						'Domain.id' => $this->passedArgs['d'],
						'Job.status' => 0
				);
			}
		}
		

		
		$parentJobs = $this->Job->find('threaded',$options);
		$parentJobs = $this->treeForCombobox($parentJobs,'Job',array('model'=>'Job','name'=>'description'),false);
		
		$groups = $this->Job->Group->find('list');
		$projects = $this->Job->Project->find('list');
		$domains = $this->Job->Domain->find('list',array('order'=>'Domain.name'));
		$users = $this->Job->User->find('list');
		$this->set(compact('projects','domains', 'users','groups','parentJobs'));
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Job->id = $id;
		if (!$this->Job->exists()) {
			throw new NotFoundException(__('Invalid job'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			$this->request->data['Job']['user_id'] = $this->Session->read('Auth.User.id');
			if ($this->Job->save($this->request->data)) {
				$this->Session->setFlash(__('The job has been saved'));
				if(isset($this->passedArgs['p']) && !empty($this->passedArgs['p'])) $this->redirect(array('controller'=>'projects','action' =>'view',$this->passedArgs['p']));
				if(isset($this->passedArgs['d']) && !empty($this->passedArgs['d'])) $this->redirect(array('controller'=>'domains','action' =>'view',$this->passedArgs['d'],'d'=>'jobs'));
				
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The job could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Job->read(null, $id);
		}
		
		$options = null;
		
		if(isset($this->passedArgs['p']) && !empty($this->passedArgs['p'])){
			// dodajemy do domeny
			$this->Job->Project->id = $this->passedArgs['p'];
			if ($this->Job->Project->exists()) {
				$this->request->data['Project']['project_id'] = $this->passedArgs['p'];
				$options['conditions'] = array(
						'Project.id' => $this->passedArgs['p']
				);
			}
		}
		
		
		if(isset($this->passedArgs['d']) && !empty($this->passedArgs['d'])){
			// dodajemy do domeny
			$this->Job->Domain->id = $this->passedArgs['d'];
			if ($this->Job->Domain->exists()) {
				$this->request->data['Domain']['Domain'] = $this->passedArgs['d'];
		
				$options['joins'] = array(
						array('table' => 'domains_jobs',
								'alias' => 'DomainsJob',
								'type' => 'inner',
								'conditions' => array(
										'Job.id = DomainsJob.job_id'
								)
						),
						array('table' => 'domains',
								'alias' => 'Domain',
								'type' => 'inner',
								'conditions' => array(
										'DomainsJob.domain_id = Domain.id'
								)
						)
				);
		
				$options['conditions'] = array(
						'Domain.id' => $this->passedArgs['d'],
						'Job.status' => 0
				);
			}
		}
		
		$parentJobs = $this->Job->find('threaded',$options);
		$parentJobs = $this->treeForCombobox($parentJobs,'Job',array('model'=>'Job','name'=>'description'),false);
		
		$groups = $this->Job->Group->find('list');
		$projects = $this->Job->Project->find('list');
		$domains = $this->Job->Domain->find('list',array('order'=>'Domain.name'));
		$users = $this->Job->User->find('list');
		$this->set(compact('projects','domains', 'users','groups','parentJobs'));
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Job->id = $id;
		if (!$this->Job->exists()) {
			throw new NotFoundException(__('Invalid job'));
		}
		if ($this->Job->delete()) {
			$this->Session->setFlash(__('Job deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Job was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
